/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.cms.guestbook.service;

import com.jeeplus.cms.util.CmsCacheUtils;
import com.jeeplus.cms.util.CmsUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.cms.column.entity.CmsColumn;
import com.jeeplus.modules.cms.guestbook.entity.CmsGuestbook;
import com.jeeplus.modules.cms.guestbook.mapper.CmsGuestbookMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 留言表Service
 *
 * @author Sawyer
 * @version 2020-11-22
 */
@Service
@Transactional(readOnly = true)
public class CmsGuestbookService extends CrudService<CmsGuestbookMapper, CmsGuestbook> {

    @Override
    public CmsGuestbook get(String id) {
        return super.get(id);
    }

    @Override
    public List<CmsGuestbook> findList(CmsGuestbook cmsGuestbook) {
        return super.findList(cmsGuestbook);
    }

    @Override
    public List<CmsGuestbook> findAllList(CmsGuestbook entity) {
        return findList(entity);
    }

    @Override
    public Page<CmsGuestbook> findPage(Page<CmsGuestbook> page, CmsGuestbook cmsGuestbook) {
        return super.findPage(page, cmsGuestbook);
    }

    @Override
    @Transactional(readOnly = false)
    public void save(CmsGuestbook cmsGuestbook) {
        // 新增记录时初始化数据
        if (null == cmsGuestbook.getId()) {
            cmsGuestbook.init();
        }

        // 设置siteId
        if (null != cmsGuestbook.getColumn() && StringUtils.isBlank(cmsGuestbook.getSiteId())) {
            CmsColumn cmsColumn = CmsUtils.getColumnById(cmsGuestbook.getColumn().getId());
            cmsGuestbook.setSiteId(null != cmsColumn ? cmsColumn.getSiteId() : null);
        }
        super.save(cmsGuestbook);
    }

    @Override
    @Transactional(readOnly = false)
    public void delete(CmsGuestbook cmsGuestbook) {
        super.delete(cmsGuestbook);
    }

    @Transactional(readOnly = false)
    public void updateExpiredWeightToReq() {
        //更新过期权重，间隔1小时
        Date updateExpiredWeightTime = (Date) CmsCacheUtils.getCache(CmsUtils.UPDATE_EXPIRED_WEIGHT_TIME_TO_GUESTBOOK_CACHE);
        if (updateExpiredWeightTime == null || (updateExpiredWeightTime.getTime() < System.currentTimeMillis())) {
            updateExpiredWeight();
            CmsCacheUtils.putCache(CmsUtils.UPDATE_EXPIRED_WEIGHT_TIME_TO_GUESTBOOK_CACHE, DateUtils.addHours(new Date(), 1));
        }
    }

    /**
     * 更新过期置顶权重
     * <p>
     * 生产时可直接设置定时任务调用此服务
     * </p>
     */
    @Transactional(readOnly = false)
    public void updateExpiredWeight() {
        mapper.updateExpiredWeight(new CmsGuestbook());
    }

    /**
     * 获取评价数据
     */
    public CmsGuestbook getEvalu(CmsGuestbook guestbook) {
        return mapper.getEvalu(guestbook);
    }

    /**
     * 点赞
     */
    @Transactional(readOnly = false)
    public void agree(CmsGuestbook guestbook) {
        mapper.agree(guestbook);
    }

    /**
     * 反对
     */
    @Transactional(readOnly = false)
    public void oppose(CmsGuestbook guestbook) {
        mapper.oppose(guestbook);
    }
}
